<template>
  <section v-loading="loading" class="zgp-handle">
    <h1>摘挂牌</h1>
    <el-form
    ref="ruleFormRef"
    label-width="auto"
    :inline="true"
  >
    <el-form-item label="申报日期：" prop="certainDate">
      <el-date-picker
        v-model="certainDate"
        type="date"
        clearable
        :editable="false"
        placeholder="请选择数据日期"
        format="YYYY-MM-DD"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        value-format="YYYY-MM-DD"
      />
    </el-form-item>
    <el-form-item label="场次类型：">
      <el-select
        v-model="reqType"
        placeholder="场次类型"
      >
        <el-option 
          v-for="item in dataTypeList" 
          :key="item.value"
          :label="item.label" 
          :value="item.value" 
        />
      </el-select>
    </el-form-item>
    <el-form-item label="当天交易场次序号：" prop="uniqueCode">
      <el-input v-model="theDateSceneIndx" type="number" placeholder="请输入" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="getData">获取</el-button>
    </el-form-item>
  </el-form>
  <div class="req-history">
    <h5>获取记录(不一定成功)::</h5>
    <div class="req-history__item" v-for="item in reqHistory" :key="item.certainDate + Math.random()">
      <div>数据日期：<span>{{ item.certainDate }}</span></div>
      <div>数据类型：<span>{{ item.reqTypes }}</span></div>
    </div>
  </div>
  </section>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import dayjs from 'dayjs';

import { getAllZGBTradePlantDetail, getSaveBuyerPublicListAndDetailThrottle, getSaveSellerPublicListAndDetailThrottle, setHandleTradeDay } from '@src/spotZcqMarketPublic/zhaiGuaPai'
import { getSpotMartetZcqReqInfo } from '@src/entries/options/generate';
import { handleState } from "@src/entries/background/spotMarketZcqReq"

const dataTypeList = [
  {
    value:'月度挂牌交易',
    label:'月度挂牌交易'
  },
  {
    value:'日挂牌交易',
    label:'日挂牌交易'
  }
]
const reqHistory = ref<{ certainDate: string, reqTypes: string }[]>([])
const reqType = ref(dataTypeList[0].value)
const theDateSceneIndx = ref(0)
const loading = ref(false)
const certainDate = ref(dayjs().format('YYYY-MM-DD'))


const getData = async () => {
  try {
    loading.value = true
    console.log(certainDate.value, reqType.value)
    setHandleTradeDay(certainDate.value, reqType.value as '月度挂牌交易'|'日挂牌交易', theDateSceneIndx.value)

    const {headers,unitid} = await getSpotMartetZcqReqInfo()

    await getAllZGBTradePlantDetail(headers, certainDate.value)

    await Promise.allSettled([
      getSaveBuyerPublicListAndDetailThrottle({headers,unitid, type:'buy'}),
      getSaveSellerPublicListAndDetailThrottle({headers,unitid, type:'sell'})
    ])

    reqHistory.value.unshift({
      certainDate: certainDate.value,
      reqTypes: `场次类型：${reqType.value}; 场次序号：${theDateSceneIndx.value}`
    })
  }catch(e) {
    console.log(e)
  }finally {
    loading.value = false
  }

}
</script>

<style lang="scss" scoped>
.zgp-handle {
  .req-history {
    margin-top: 30px;
    &__item {
      margin-bottom: 22px
    }
  }
  :deep(.el-form) {
    .el-select {
      width: initial;
      min-width:200px;
    }
  }
}
</style>